<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="替代单号" prop="replacementNo">
        <el-input
          v-model="queryParams.replacementNo"
          placeholder="请输入替代单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="替代类型" prop="substitutionType">
        <el-input
          v-model="queryParams.substitutionType"
          placeholder="请输入替代类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编码" prop="goodsCode">
        <el-input
          v-model="queryParams.goodsCode"
          placeholder="请输入物料编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入物料名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specificationModel">
        <el-input
          v-model="queryParams.specificationModel"
          placeholder="请输入规格型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="辅助属性" prop="auxiliaryProperties">
        <el-input
          v-model="queryParams.auxiliaryProperties"
          placeholder="请输入辅助属性"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键物料" prop="keyMaterials">
        <el-input
          v-model="queryParams.keyMaterials"
          placeholder="请输入关键物料"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用量" prop="consumption">
        <el-input
          v-model="queryParams.consumption"
          placeholder="请输入用量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="取代" prop="replaces">
        <el-input
          v-model="queryParams.replaces"
          placeholder="请输入取代"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="混用" prop="mixedUse">
        <el-input
          v-model="queryParams.mixedUse"
          placeholder="请输入混用"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否通用B" prop="universal">
        <el-input
          v-model="queryParams.universal"
          placeholder="请输入是否通用B"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否组合替" prop="combinatorialSubstitution">
        <el-input
          v-model="queryParams.combinatorialSubstitution"
          placeholder="请输入是否组合替"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核" prop="examine">
        <el-input
          v-model="queryParams.examine"
          placeholder="请输入审核"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用" prop="apply">
        <el-input
          v-model="queryParams.apply"
          placeholder="请输入使用"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制单人" prop="producer">
        <el-input
          v-model="queryParams.producer"
          placeholder="请输入制单人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制单日期" prop="preparationDate">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.preparationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择制单日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核人" prop="auditor">
        <el-input
          v-model="queryParams.auditor"
          placeholder="请输入审核人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核日期" prop="auditDate">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.auditDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择审核日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="ISPML" prop="ispml">
        <el-input
          v-model="queryParams.ispml"
          placeholder="请输入ISPML"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BOM单编号" prop="bomNo">
        <el-input
          v-model="queryParams.bomNo"
          placeholder="请输入BOM单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:replace:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:replace:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:replace:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:replace:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="replaceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料替代关系id" align="center" prop="replaceId" />
      <el-table-column label="替代单号" align="center" prop="replacementNo" />
      <el-table-column label="替代类型" align="center" prop="substitutionType" />
      <el-table-column label="物料编码" align="center" prop="goodsCode" />
      <el-table-column label="物料名称" align="center" prop="goodsName" />
      <el-table-column label="规格型号" align="center" prop="specificationModel" />
      <el-table-column label="辅助属性" align="center" prop="auxiliaryProperties" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="关键物料" align="center" prop="keyMaterials" />
      <el-table-column label="用量" align="center" prop="consumption" />
      <el-table-column label="取代" align="center" prop="replaces" />
      <el-table-column label="混用" align="center" prop="mixedUse" />
      <el-table-column label="是否通用B" align="center" prop="universal" />
      <el-table-column label="是否组合替" align="center" prop="combinatorialSubstitution" />
      <el-table-column label="审核" align="center" prop="examine" />
      <el-table-column label="使用" align="center" prop="apply" />
      <el-table-column label="制单人" align="center" prop="producer" />
      <el-table-column label="制单日期" align="center" prop="preparationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.preparationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditor" />
      <el-table-column label="审核日期" align="center" prop="auditDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="ISPML" align="center" prop="ispml" />
      <el-table-column label="BOM单编号" align="center" prop="bomNo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:replace:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:replace:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料替代关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="替代单号" prop="replacementNo">
          <el-input v-model="form.replacementNo" placeholder="请输入替代单号" />
        </el-form-item>
        <el-form-item label="替代类型" prop="substitutionType">
          <el-input v-model="form.substitutionType" placeholder="请输入替代类型" />
        </el-form-item>
        <el-form-item label="物料编码" prop="goodsCode">
          <el-input v-model="form.goodsCode" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="规格型号" prop="specificationModel">
          <el-input v-model="form.specificationModel" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="辅助属性" prop="auxiliaryProperties">
          <el-input v-model="form.auxiliaryProperties" placeholder="请输入辅助属性" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="关键物料" prop="keyMaterials">
          <el-input v-model="form.keyMaterials" placeholder="请输入关键物料" />
        </el-form-item>
        <el-form-item label="用量" prop="consumption">
          <el-input v-model="form.consumption" placeholder="请输入用量" />
        </el-form-item>
        <el-form-item label="取代" prop="replaces">
          <el-input v-model="form.replaces" placeholder="请输入取代" />
        </el-form-item>
        <el-form-item label="混用" prop="mixedUse">
          <el-input v-model="form.mixedUse" placeholder="请输入混用" />
        </el-form-item>
        <el-form-item label="是否通用B" prop="universal">
          <el-input v-model="form.universal" placeholder="请输入是否通用B" />
        </el-form-item>
        <el-form-item label="是否组合替" prop="combinatorialSubstitution">
          <el-input v-model="form.combinatorialSubstitution" placeholder="请输入是否组合替" />
        </el-form-item>
        <el-form-item label="审核" prop="examine">
          <el-input v-model="form.examine" placeholder="请输入审核" />
        </el-form-item>
        <el-form-item label="使用" prop="apply">
          <el-input v-model="form.apply" placeholder="请输入使用" />
        </el-form-item>
        <el-form-item label="制单人" prop="producer">
          <el-input v-model="form.producer" placeholder="请输入制单人" />
        </el-form-item>
        <el-form-item label="制单日期" prop="preparationDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.preparationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择制单日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人" prop="auditor">
          <el-input v-model="form.auditor" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="审核日期" prop="auditDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.auditDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择审核日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="ISPML" prop="ispml">
          <el-input v-model="form.ispml" placeholder="请输入ISPML" />
        </el-form-item>
        <el-form-item label="BOM单编号" prop="bomNo">
          <el-input v-model="form.bomNo" placeholder="请输入BOM单编号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReplace, getReplace, delReplace, addReplace, updateReplace, exportReplace } from "@/api/base/replace";

export default {
  name: "Replace",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料替代关系表格数据
      replaceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        replacementNo: undefined,
        substitutionType: undefined,
        goodsCode: undefined,
        goodsName: undefined,
        specificationModel: undefined,
        auxiliaryProperties: undefined,
        unit: undefined,
        keyMaterials: undefined,
        consumption: undefined,
        replaces: undefined,
        mixedUse: undefined,
        universal: undefined,
        combinatorialSubstitution: undefined,
        examine: undefined,
        apply: undefined,
        producer: undefined,
        preparationDate: undefined,
        auditor: undefined,
        auditDate: undefined,
        ispml: undefined,
        bomNo: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料替代关系列表 */
    getList() {
      this.loading = true;
      listReplace(this.queryParams).then(response => {
        this.replaceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        replaceId: undefined,
        replacementNo: undefined,
        substitutionType: undefined,
        goodsCode: undefined,
        goodsName: undefined,
        specificationModel: undefined,
        auxiliaryProperties: undefined,
        unit: undefined,
        keyMaterials: undefined,
        consumption: undefined,
        replaces: undefined,
        mixedUse: undefined,
        universal: undefined,
        combinatorialSubstitution: undefined,
        examine: undefined,
        apply: undefined,
        producer: undefined,
        preparationDate: undefined,
        auditor: undefined,
        auditDate: undefined,
        ispml: undefined,
        bomNo: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.replaceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料替代关系";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const replaceId = row.replaceId || this.ids
      getReplace(replaceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料替代关系";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.replaceId != null) {
            updateReplace(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReplace(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const replaceIds = row.replaceId || this.ids;
      this.$confirm('是否确认删除物料替代关系编号为"' + replaceIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReplace(replaceIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有物料替代关系数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportReplace(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
